#include "client.h"

int client_worker(int netfd, char *buf)
{
    char command[8];
    char path[1024];
    
    int length = 0;

    char *command_new = strtok(buf, " ");// 存放用户发送的“命令 路径/文件”中的“命令”
    char *path_new = strtok(NULL, "\n");// 存放用户发送“命令 路径/文件”中的“路径/文件”
    bzero(command, sizeof(command));// 清空command
    bzero(path, sizeof(path));// 清空path
    if (path_new == NULL)// 若用户只发送“命令”未跟路径或文件，如“ls”、“pwd”
    {
        strncpy(command, command_new, strlen(command_new) - 1);// 赋值command（去除\n）
    }
    else
    {
        strcpy(command, command_new);// 赋值command
        strcpy(path, path_new);// 赋值path
    }

    //判断命令
    if (strcmp(command, "puts") == 0)
    {
        printf("----worker_puts_client----\n");
        worker_puts_client(netfd, path);
    }
    else if (strcmp(command, "gets") == 0)
    {
        printf("----worker_gets_client----\n");
        worker_gets_client(netfd);
    }
    else if (strcmp(command, "cd") == 0 || strcmp(command, "ls") == 0 || strcmp(command, "remove") == 0 || 
            strcmp(command, "pwd") == 0 || strcmp(command, "mkdir") == 0 || strcmp(command, "rmdir") == 0 )
    {

        int length = 0;
        printf("----worker_%s----\n", command);
        recv(netfd, &length, sizeof(length), MSG_WAITALL);
        int total = 0;
        char *p = (char *)buf;
        while(total < length){
        ssize_t sret = recv(netfd,p+total,length-total,0);
        total += sret;
    }
        printf(" %s \n", buf);
        printf(" %d \n", length);
    }
    else
    {
        printf("----Command is illegal!----\n----Please input again.----\n");
        return -1;
    }
    return 0;
}

// 王
int worker_gets_client(int netfd)
{
    printf("//gets start\n");
    train_t train;
    bzero(&train,sizeof(train));
    recvn(netfd,&train.length,sizeof(train.length));
    recvn(netfd,train.data,train.length);

    char *path = strtok(train.data, "\r\n");
    int fd = open(path,O_RDWR|O_TRUNC|O_CREAT,0666);

    FILE *fp = fdopen(fd, "r+");

    struct stat statbuf;
    fstat(fd,&statbuf);

    fseek(fp,statbuf.st_size,0);   //偏移到文件末尾  
    printf("fseek over \n");                    //
    send(netfd,&statbuf.st_size,sizeof(statbuf.st_size),0); //发送文件长度
    printf("send over\n");                      //
    off_t filesize;
    recvn(netfd,&train.length,sizeof(train.length));
    recvn(netfd,train.data,train.length);
    memcpy(&filesize,train.data,sizeof(off_t));
    printf("filesize = %ld\n", filesize);
    ftruncate(fd,filesize);
    char *p = (char *)mmap(NULL,filesize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    ERROR_CHECK(p,MAP_FAILED,"mmap");
    recvn(netfd,p,filesize);
    munmap(p,filesize);
    return 0;
}
int recvn(int sockfd,void *buf, int length){
    int total = 0;
    char *p = (char *)buf;
    while(total < length){
        ssize_t sret = recv(sockfd,p+total,length-total,0);
        total += sret;
    }
    return 0;
}


int worker_puts_client(int netfd,char *filename){
    
    int mdLen=16;

    printf("9:\n");
    unsigned char md5[17];
    //md5sum_produce((const char *)buf,&md5);
    int fd1= open(filename,O_RDONLY);
    MD5_CTX ctx;
    MD5_Init(&ctx);
    char buf[4096];

    while(1){
        bzero(buf,sizeof(buf));
        ssize_t sret = read(fd1,buf,sizeof(buf));
        if(sret == 0){
            break;
        }
        MD5_Update(&ctx,buf,sret);
    }

    unsigned char md[17];//将要保存md5码16个字节的二进制数据
    md[17]='\0';          //成为字符串
    MD5_Final(md,&ctx);
    printf("md5:");
    for(int i = 0; i < 16; ++i){
        printf("%02x",md[i]);
    }
    printf("\n");
    //printf("md5:%s\n",md);  
    //2 通过小火车netfd传输md5d码给服务端
    
    train_t train;
    train.length = 0;
    bzero(train.data, sizeof(train.data));

    train.length=mdLen;
    strcpy(train.data,md);

    
    send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);

    // 接受状态码
    bzero(&train, sizeof(train));
    recv(netfd, &train.length, sizeof(train.length), MSG_WAITALL);
    recv(netfd, &train.data, train.length, MSG_WAITALL);
    printf("status code :%d\n", train.length);









    if(train.length==1||train.length==2){
        printf("send over!\n");           
        return 0;
    }
    else if(train.length==3){
        bzero(&train, sizeof(train));
        int fd = open(filename, O_RDONLY);
        while(1)
        {
            ssize_t sret = read(fd, train.data, sizeof(train.data));
            if(sret == 0)
            {
                break;
            }
            train.length = sret;
            sret = send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
            if(train.length != 1000)
            {
                printf("train.length = %d\n", train.length);
            }
        }
    train.length = 0;
    send(netfd, &train, sizeof(train.length), MSG_NOSIGNAL);
    // 传输完成
    return 0;


    }










    

}




